package com.taichuan.selfcheck.ad;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by hyC on 2020/1/7 11:38
 */
public class ADView extends FrameLayout implements ADViewInterface {
    private static final String TAG = ADView.class.getSimpleName();
    private LayoutParams mLayoutMatchParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    private LayoutParams mLayoutWrapParams = new LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    private VideoSurfaceView mVideoSurfaceView;
    private boolean updatedLoad = false;
    private boolean resumed = true;

    private ADViewPresenter mPresenter;


    private Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            mPresenter.setUpdatedLoad(updatedLoad);
            if (getParent() != null && resumed) {
                Log.d(getClass().getSimpleName(), "handleMessage: " + ADView.this + " , resumed : " + resumed);
                resume();
            }
            return false;
        }
    });


    public ADView(@NonNull Context context) {
        super(context);
        setBackgroundColor(Color.BLACK);
        setVisibility(View.GONE);
    }

    public void init(ADArea area) {
        mPresenter = new ADViewPresenter(getContext(), this, area);
    }

    public void setUpdatedLoad(boolean updatedLoad) {//数据有更新或者不播放视频时调用
        this.updatedLoad = updatedLoad;
        mHandler.sendEmptyMessage(0);
    }

    public boolean isPlayAD() {
        return mPresenter.isPlayAD();
    }

    public boolean isPlayingVideo() {
        return mVideoSurfaceView != null && getChildAt(0) == mVideoSurfaceView;
    }

    public void stopPlayVideo() {
        mPresenter.setStopPlayVideo(true);
    }

    public void resume() {
        Log.e(TAG, "resume: 恢复广告播放");
        resumed = true;
        mPresenter.resume();
    }

    public void pause() {
        Log.e(TAG, "pause: 暂停广告播放");
        resumed = false;
        mPresenter.pause();
    }

    public ADView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ADView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean playVideoSuccess() {
        return false;
    }

    @Override
    public void dismissGrallery() {

    }

    @Override
    public void showSurfaceView() {
        Log.e(TAG, "showSurfaceView："+mVideoSurfaceView);
        if (getVisibility() == View.GONE) {
            setVisibility(View.VISIBLE);
        }
        if (mVideoSurfaceView == null) {
            mVideoSurfaceView = new VideoSurfaceView(getContext(), mPresenter.getVideoPresenter());
        }
        Log.e(TAG, "showSurfaceView：mVideoSurfaceView.getParent():"+mVideoSurfaceView.getParent());
        if (mVideoSurfaceView.getParent() != null) {
            Log.e(TAG, "showSurfaceView：removeView");
            ViewParent viewParent = mVideoSurfaceView.getParent();
            ((ViewGroup) viewParent).removeView(mVideoSurfaceView);
        }
        addView(mVideoSurfaceView, 0, mLayoutWrapParams);
        Log.e(TAG, "showSurfaceView：addView");
    }

    @Override
    public void dismissSurfaceView() {
        if (mVideoSurfaceView != null && mVideoSurfaceView.getParent() != null) {
            removeView(mVideoSurfaceView);
        }
    }

    @Override
    public void dismissADView() {
        if (getVisibility() == View.VISIBLE) {
            setVisibility(View.GONE);
        }
    }

    @Override
    public boolean isADViewSurvival() {
        return getParent() != null && resumed;
    }

    @Override
    public void resetNewMediaData() {
        mPresenter.resetNMImageData();
    }
}
